简介

Redis是一个开源(BSD许可),内存存储的数据结构服务器,是一个高性能的key-value数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

下载与安装

wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make
make isntall

服务管理

通过指定配置文件启动

sudo redis-server /opt/redis-4.0.1/redis.conf

22090:C 20 Sep 09:52:39.951 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22090:C 20 Sep 09:52:39.951 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=22090, just started
22090:C 20 Sep 09:52:39.951 # Configuration loaded
如果输出如上信息,证明Redis服务已经启动成功。

通过命令redis-server 启动

redis-server

关闭Redis服务

redis-cli shutdown

客户端登陆并验证

redis-cli

127.0.0.1:6379> set a b
OK
127.0.0.1:6379> get a
"b"
127.0.0.1:6379> exit

设置redis开机启动

创建Redis配置目录

sudo mkdir /etc/redis

拷贝配置文件

sudo cp /opt/redis-4.0.1/redis.conf /etc/redis/

修改目录/etc/redis/下面的redis.conf文件,将以下内容

> daemonize no
> logfile ""

修改为:

#打开后台运行选项
daemonize yes
#设置日志文件路径
logfile "/var/log/redis/redis.log"

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

PATH=/usr/local/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed."
        else
            echo "Starting Redis server..."
            $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
            echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is not running."
        else
            PID=$(cat $PIDFILE)
            echo "Stopping..."
            $REDIS_CLI -p $REDISPORT SHUTDOWN
            while [ -x $PIDFILE ]
            do
                echo "Waiting for Redis to shutdown..."
                sleep 1
            done
            echo "Redis stopped"
        fi
        ;;
    restart|force-reload)  
        ${0} stop  
        ${0} start
        ;;
    *)
        echo "Usage: /etc/init.d/redis {start|stop|restart|fore-reload}"
        exit 1
esac

问题一

insserv: warning: script 'K01redis' missing LSB tags and overrides

解决方法
安装一个中文语言,系统就知道zh_CN.UTF-8了,这个时候用perl就不会报错了

sudo apt-get install language-pack-zh-hans

问题二

insserv: warning: script 'redis' missing LSB tags and overrides

解决方法
修改目录/etc/init.d/下面的redis文件,在文件头部#!/bin/sh下面添加如下内容:

### BEGIN INIT INFO
# Provides: OSSEC HIDS
# Required-Start: $network $remote_fs $syslog $time
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OSSEC HIDS
### END INIT INFO

使用脚本启动服务

  • 开启redis:service redis start
  • 停止redis: service redis stop
  • 重启redis: service redis restart
  • 查看服务状态:service redis status

刘校
3 声望0 粉丝